package com.py.korona.ui.activities.room;

import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RatingBar;
import com.py.korona.R;
import com.py.korona.ui.controls.CustomMultiSpinner;
import com.py.korona.ui.controls.fragments.CustomFragment;
import cz.destil.settleup.gui.MultiChoiceItem;
import cz.destil.settleup.gui.MultiSpinner.MultiSpinnerListener;

public class RoomDetailFragmentEditor extends CustomFragment implements MultiSpinnerListener {
    private static final int           SELECT_PICTURE = 1;
    // private Uri selectedImage;
    private Bitmap                     bitmap;
    private List<MultiChoiceItem>      changedList;
    private RoomDetailFragmentListener listener;

    public RoomDetailFragmentListener getListener() {
        return listener;
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (SELECT_PICTURE == requestCode) {
            if (Activity.RESULT_OK == resultCode) {
                Uri uri = intent.getData();
                if (uri != null) {
                    try {
                        InputStream inputStream = getActivity().getContentResolver().openInputStream(uri);
                        bitmap = BitmapFactory.decodeStream(inputStream);
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } else {
                    bitmap = (Bitmap) intent.getExtras().get("data");
                }
                ImageView imageView = (ImageView) getActivity().findViewById(R.id.activity_room_fragment_edit_image_view);
                if (null == bitmap) {
                    imageView.setImageResource(android.R.color.transparent);
                } else {
                    imageView.setImageBitmap(bitmap);
                }
            }
        } else {
            super.onActivityResult(requestCode, resultCode, intent);
        }
    }

    @Override
    public void onItemsHadSelected(Map<Serializable, MultiChoiceItem> selected) {
        changedList = new ArrayList<MultiChoiceItem>();
        if (null != selected) {
            for (Serializable key : selected.keySet()) {
                changedList.add(selected.get(key));
            }
        }
    }

    @Override
    public int optionsMenuResourceId() {
        return R.menu.activity_room_detail_edit;
    }

    public void setListener(RoomDetailFragmentListener listener) {
        this.listener = listener;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.DetailsMenuItemSave: {
                /*
                 * save name
                 */
                EditText mTitleText = (EditText) getActivity().findViewById(R.id.activity_room_fragment_edit_edit_name);
                String name = mTitleText.getText().toString();
                listener.fragmentSetRoomName(this, name);
                /*
                 * save description
                 */
                EditText mTitleDescription = (EditText) getActivity().findViewById(R.id.activity_room_fragment_edit_edit_description);
                String description = mTitleDescription.getText().toString();
                listener.fragmentSetRoomDescription(this, description);
                /*
                 * save rating
                 */
                RatingBar mTitleRating = (RatingBar) getActivity().findViewById(R.id.activity_room_fragment_edit_rating);
                float rating = mTitleRating.getRating();
                listener.fragmentSetRoomRating(this, rating);
                /*
                 * save categories
                 */
                listener.fragmentSetRoomCategories(this, changedList);
                /*
                 * save image
                 */
                listener.fragmentSetRoomBitmap(this, bitmap);
                /*
                 * save
                 */
                listener.fragmentOnSave(this);
                listener.fragmentOnCloseAction(this);
                return true;
            }
            case R.id.DetailsMenuItemDelete: {
                listener.fragmentOnDeleteAction(this);
                listener.fragmentOnCloseAction(this);
                return true;
            }
            case R.id.DetailsMenuItemCancel: {
                listener.fragmentOnReadonlyAction(this);
                return true;
            }
            case R.id.DetailsMenuItemEdit: {
                listener.fragmentOnEditAction(this);
                return true;
            }
            case android.R.id.home: {
                listener.fragmentOnCloseAction(this);
                return true;
            }
            default: {
                return super.onOptionsItemSelected(item);
            }
        }
    }

    @Override
    public void viewDidLoad() {
        /*
         * init events for options
         */
        ImageView imageView = (ImageView) getActivity().findViewById(R.id.activity_room_fragment_edit_image_view);
        imageView.setOnTouchListener(new OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                Intent pickIntent = new Intent();
                pickIntent.setType("image/*");
                pickIntent.setAction(Intent.ACTION_GET_CONTENT);
                // takePhotoIntent
                Intent takePhotoIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                // takePhotoIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT,
                // android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
                String pickTitle = "Select or take a new Picture"; // Or get
                                                                   // from
                                                                   // strings.xml
                // chooserIntent
                Intent chooserIntent = Intent.createChooser(pickIntent, pickTitle);
                chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] {
                    takePhotoIntent
                });
                //
                startActivityForResult(chooserIntent, SELECT_PICTURE);
                return false;
            }
        });
        bitmap = listener.fragmentGetRoomBitmap(this);
        if (null == bitmap) {
            imageView.setImageResource(android.R.color.transparent);
        } else {
            imageView.setImageBitmap(bitmap);
        }
        /* load name */
        EditText mTitleText = (EditText) getActivity().findViewById(R.id.activity_room_fragment_edit_edit_name);
        mTitleText.setText(listener.fragmentGetRoomName(this));
        /* load description */
        EditText mTitleDescription = (EditText) getActivity().findViewById(R.id.activity_room_fragment_edit_edit_description);
        mTitleDescription.setText(listener.fragmentGetRoomDescription(this));
        /* load rating */
        RatingBar mTitleRating = (RatingBar) getActivity().findViewById(R.id.activity_room_fragment_edit_rating);
        mTitleRating.setRating(listener.fragmentGetRoomRating(this));
        /* load data spiner */
        List<MultiChoiceItem> list = listener.fragmentGetRoomCategoriesList(this);
        CustomMultiSpinner detailsEditCategorySpinner = (CustomMultiSpinner) getActivity().findViewById(R.id.activity_room_fragment_edit_edit_category);
        detailsEditCategorySpinner.setItems(list, "tap to select category", this);
    }

    @Override
    public int viewLayoutId() {
        return R.layout.activity_room_detail_fragment_edit;
    }
}
